﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace winform基础学习
{
    public partial class EditForm : Form
    {
        string userName;

        private Action<object, EventArgs> loadDelegate;
        public EditForm(string userName, Action<object, EventArgs> loadDelegate)
        {
            this.userName = userName;
            InitializeComponent();
            label1.Text = $"编辑对象{userName}";
            this.loadDelegate = loadDelegate;
        }
        UserTModel model = null;
        private void EditForsm_Load(object sender, EventArgs e)
        {
            
            using (MyDbContext myDbContext = new MyDbContext())
            {
                model= myDbContext.userTModels.FirstOrDefault(s => s.UserName == userName);
            }
            textBox1.Text = model.NickName;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (MyDbContext myDbContext = new MyDbContext())
            {
                model.NickName=textBox1.Text;
                myDbContext.userTModels.Attach(model);
                myDbContext.Entry(model).State=System.Data.Entity.EntityState.Modified;
                myDbContext.SaveChanges();
            }
            this.Close();

            loadDelegate(sender, e);
        }
    }
}
